﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iCollect.Events;
using iCollect.Constants;

namespace iCollect
{
    public partial class Form2 : Form
    {
        public const string NORMAL = "normal";
        public const string EXIT = "exit";
        public event EventHandler<CustomEventArgs> RaiseCustomEvent;

        private static Form2 instance;

        public Form2()
        {
            InitializeComponent();
        }

        public static Form2 getInstance()
        {
            if (instance == null)
            {
                instance = new Form2();
            }
            return instance;
        }


        protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
        {
            EventHandler<CustomEventArgs> handler = RaiseCustomEvent;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private BindingSource source1;
        private bool dataBindingCompleted = false;
        private bool formLoaded = false;

        private void dataGridView1_DataBindingComplete(object sender,
    DataGridViewBindingCompleteEventArgs e)
        {
            source1 = (BindingSource)dataGridView1.DataSource;
            dataBindingCompleted = true;
            setEditStatus();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                return;
            }
            string sText = comboBox1.SelectedItem.ToString();
            if (comboBox1.SelectedIndex != 0)
            {
                source1.Filter = "type = '" + (comboBox1.SelectedIndex - 1) + "'";
            }
            else
            {
                source1.Filter = null;
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“cUPDataDataSet.devicename”中。您可以根据需要移动或移除它。
            this.devicenameTableAdapter.Fill(this.cUPDataDataSet.devicename);
            // TODO: 这行代码将数据加载到表“cUPDataDataSet.devicename”中。您可以根据需要移动或移除它。
            this.devicenameTableAdapter.Fill(this.cUPDataDataSet.devicename);
            // TODO: 这行代码将数据加载到表“cUPDataDataSet.device”中。您可以根据需要移动或移除它。
            this.deviceTableAdapter.Fill(this.cUPDataDataSet.device);
            formLoaded = true;
            setEditStatus();
        }

        private void setEditStatus()
        {
            if (!dataBindingCompleted)
            {
                return;
            }
            if (!formLoaded)
            {
                return;
            }
            if (GlobalConstants.isAdmin)
            {
                editData();
            }
            else
            {
                bindingNavigator1.Visible = false;
                endEditData();
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count == 0)
            {
                popupAlert("请选择设备！");
                return;
            }

            //code
            setDevice();
            
            OnRaiseCustomEvent(new CustomEventArgs(NORMAL, 0));
            this.Hide();
        }

        private void setDevice()
        {
            DataGridViewCellCollection cells = dataGridView1.SelectedRows[0].Cells;
            GlobalConstants.currentDevice = cells[0].Value.ToString();
            GlobalConstants.type = cells[1].Value.ToString();
            GlobalConstants.typeLabel = cells[1].FormattedValue.ToString();
            GlobalConstants.assetCode = cells[3].Value.ToString();
            GlobalConstants.sDate = (DateTime)(cells[4].Value);
            GlobalConstants.owner = cells[5].Value.ToString();

        }

        private void popupAlert(string msg)
        {
            DialogResult MsgBoxResult;//设置对话框的返回值
            MsgBoxResult = MessageBox.Show(msg,//对话框的显示内容
            "提示",//对话框的标题
            MessageBoxButtons.OK,//定义对话框的按钮，这里定义了YSE和NO两个按钮
            MessageBoxIcon.Exclamation,//定义对话框内的图表式样，这里是一个黄色三角型内加一个感叹号
            MessageBoxDefaultButton.Button1);//定义对话框的按钮式样
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            editData();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (!cUPDataDataSet.HasChanges())
            {
                return;
            }
            try
            {
                //deviceBindingSource.EndEdit();
                deviceTableAdapter.Update(this.cUPDataDataSet.device);
                this.cUPDataDataSet.AcceptChanges();
                //endEditData();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Update failed:" + ex.Message);

            }

        }

        private void editData()
        {
            dataGridView1.ReadOnly = false;
            this.bindingNavigatorAddNewItem.Enabled = true;
            this.bindingNavigatorDeleteItem.Enabled = true;
        }

        private void endEditData()
        {
            dataGridView1.ReadOnly = true;
            this.bindingNavigatorAddNewItem.Enabled = false;
            this.bindingNavigatorDeleteItem.Enabled = false;
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            OnRaiseCustomEvent(new CustomEventArgs(NORMAL, 0));
            this.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
